package com.ydsungan.web;
import com.ydsungan.bean.Signin;
import com.ydsungan.bean.Teacher;
import com.ydsungan.service.SigninService;
import com.ydsungan.serviceimpl.SigninServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HistoryServlet extends HttpServlet {

    private SigninService signinService = null;
    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Override
    public void init() throws ServletException {
        try {
            signinService = new SigninServiceImpl();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        //查询该老师的所有的发布历史
        Teacher teacher = (Teacher) this.getServletContext().getAttribute("loginteacher");
        if(teacher == null){
            response.sendRedirect("login-teacher.jsp");
        }else{
            Map map = new HashMap();
            map.put("organiser", teacher.getId());
            List<Signin> list = signinService.getSigninByMap(map);
            for(Signin signin : list){
                String releaseTime = signin.getReleaseTime();
                String [] words = releaseTime.split(" ")[0].substring(5).split("-");
                String newReleaseTime = words[0] + "月" + words[1] + "日" + " " + releaseTime.split(" ")[1];
                signin.setReleaseTime(newReleaseTime);
                signin.setSigninDay(signin.getSigninDay().substring(5));
            }
            this.getServletContext().setAttribute("signinList",list);
            this.getServletContext().setAttribute("signinListSize",list.size());
            response.sendRedirect("wait.jsp");
        }




    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

}
